1 /*
2 * Copyright (C) 2009 The Guava Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11 * express or implied. See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14
15 package com.google.common.collect;
16
17 import com.google.common.annotations.GwtCompatible;
18 import com.google.common.annotations.GwtIncompatible;
19
20 import java.util.Comparator;
21
22 import javax.annotation.Nullable;
23
24 /**
25 * List returned by {@code ImmutableSortedSet.asList()} when the set isn't empty.
26 *
27 * @author Jared Levy
28 * @author Louis Wasserman
29 */
30 @GwtCompatible(emulated = true)
31 @SuppressWarnings("serial")
32 final class ImmutableSortedAsList<E> extends RegularImmutableAsList<E>
33 implements SortedIterable<E> {
34 ImmutableSortedAsList(
35 ImmutableSortedSet<E> backingSet, ImmutableList<E> backingList) {
36 super(backingSet, backingList);
37 }
38
39 @Override
40 ImmutableSortedSet<E> delegateCollection() {
41 return (ImmutableSortedSet<E>) super.delegateCollection();
42 }
43
44 @Override public Comparator<? super E> comparator() {
45 return delegateCollection().comparator();
46 }
47
48 // Override indexOf() and lastIndexOf() to be O(log N) instead of O(N).
49
50 @GwtIncompatible("ImmutableSortedSet.indexOf")
51 // TODO(cpovirk): consider manual binary search under GWT to preserve O(log N) lookup
52 @Override public int indexOf(@Nullable Object target) {
53 int index = delegateCollection().indexOf(target);
54
55 // TODO(kevinb): reconsider if it's really worth making feeble attempts at
56 // sanity for inconsistent comparators.
57
58 // The equals() check is needed when the comparator isn't compatible with
59 // equals().
60 return (index >= 0 && get(index).equals(target)) ? index : -1;
61 }
62
63 @GwtIncompatible("ImmutableSortedSet.indexOf")
64 @Override public int lastIndexOf(@Nullable Object target) {
65 return indexOf(target);
66 }
67
68 @Override
69 public boolean contains(Object target) {
70 // Necessary for ISS's with comparators inconsistent with equals.
71 return indexOf(target) >= 0;
72 }
73
74 @GwtIncompatible("super.subListUnchecked does not exist; inherited subList is valid if slow")
75 /*
76 * TODO(cpovirk): if we start to override indexOf/lastIndexOf under GWT, we'll want some way to
77 * override subList to return an ImmutableSortedAsList for better performance. Right now, I'm not
78 * sure there's any performance hit from our failure to override subListUnchecked under GWT
79 */
80 @Override
81 ImmutableList<E> subListUnchecked(int fromIndex, int toIndex) {
82 return new RegularImmutableSortedSet<E>(
83 super.subListUnchecked(fromIndex, toIndex), comparator())
84 .asList();
85 }
86 }